package com.hoshiicloud.mallshop.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.query.OmnipotentWrapper;
import com.hoshiicloud.common.query.UploadFileResponse;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.goods.entity.spu.ExtSpu;
import com.hoshiicloud.mallshop.feign.ShopFileUploadService;
import com.hoshiicloud.mallshop.feign.ShopGoodsSpuSkuFeignService;
import com.hoshiicloud.mallshop.feign.ShopMarketFeignService;
import com.hoshiicloud.mallshop.service.StoreSlideshowService;
import com.hoshiicloud.market.entity.activities.Slideshow;
import com.hoshiicloud.market.entity.activities.dto.SlideshowDto;
import com.hoshiicloud.market.entity.activities.param.SlideshowQueryParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @author linjx on 2019/8/29
 */
@Service
@Slf4j
public class StoreSlideshowServiceImpl implements StoreSlideshowService {

    @Autowired
    private ShopFileUploadService fileUpload;
    @Autowired
    private ShopMarketFeignService marketFeignService;
    @Autowired
    private ShopGoodsSpuSkuFeignService goodsService;

    /**
     * 文件上传
     */
    @Override
    public UploadFileResponse uploadFile(MultipartFile file) {

        log.info("文件名:"+file.getOriginalFilename());
        String bucketName = "obs-7d37";
        String objectKey = file.getOriginalFilename();
        String fileName = file.getOriginalFilename();
        String folder = "slideshow";
        Boolean privateOwned = false;
        Response<UploadFileResponse> response = fileUpload.fileUpload(bucketName, objectKey, fileName, folder, privateOwned, file);
        log.info(response.toString());
        return response.getData();
    }

    /**
     * 查询需要跳转的商品信息
     */
    @Override
    public List<ExtSpu> chooseExtSpus(String title, Long storeId) {

        List<OmnipotentWrapper> lists = new ArrayList<>();
        if (StringUtils.isNotBlank(title)) {
            lists.add(OmnipotentWrapper.setWrapper("like", "title", title));
        }
        lists.add(OmnipotentWrapper.setWrapper("eq", "store_id", storeId));
        lists.add(OmnipotentWrapper.setWrapper("eq", "deleted", 0));
        return goodsService.queryOmnipotentExtSpu(lists).getData();
    }


    /**
     * 分页查询商品信息
     */
    @Override
    public Page<SlideshowDto> slidershowPage(SlideshowQueryParam param) {
        Page<Slideshow> data = marketFeignService.slidershowPage(param).getData();

        Page<SlideshowDto> page = new Page<>();
        page.setTotal(data.getTotal());
        page.setSize(data.getSize());
        page.setCurrent(data.getCurrent());
        page.setPages(data.getPages());

        List<Slideshow> records = marketFeignService.slidershowPage(param).getData().getRecords();
        List<SlideshowDto> slideshowDtos = new ArrayList<>();
        records.stream().forEach(s -> {
            try {
                SlideshowDto slideshowDto = BeanUtils.covertType(s, SlideshowDto.class);
                if (s.getJumpType() == 1) {
                    ExtSpu extSpu = goodsService.getExtSpuById(Long.parseLong(s.getJumpUrl() + "")).getData();

                    slideshowDto.setGoodsName(extSpu.getTitle());
                }
                slideshowDtos.add(slideshowDto);
            } catch (Exception e) {
               log.error("分页查询店铺轮播图信息失败:id{},名称{},错误{}", s.getId(), s.getName(), e.getMessage());
                e.printStackTrace();
            }
        });
        page.setRecords(slideshowDtos);
        return page;
    }


}
